
const app = getApp();
import requestUtil from '../../../utils/requestUtil';
import _DuoguanData from '../../../utils/data';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    ttype: 0,
    end_time: '2018-03-22 23:23:23', //活动截至时间
    // is_end: 0, //活动是否结束
    this_page_size: 1,
    this_page_num: 10,
    is_load_more: true,
    c_data: [],  //排行榜数据
    my_top: 0,
    p_num: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let active_id = options.active_id ? options.active_id : 13;
    this.setData({active_id:active_id});
    requestUtil.get(_DuoguanData.duoguan_host_api_url + "/index.php/addon/DuoguanWenda/ApiNew/get_config.html", {active_id:active_id}, (data) => {
      //活动未开始，跳转到首页
      this.setData({
        end_time: data.end_time,
        is_end: data.is_end,
        ranking: data.ranking,
        info: data
      });

      if (data.is_start == 0) {
        wx.showModal({
          title: '提示',
          content: '当前赛季暂未开启！',
          success: function (res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../index/index'
              })
              console.log('用户点击确定')
            } else if (res.cancel) {
              wx.switchTab({
                url: '../index/index'
              })
              console.log('用户点击取消')
            }
          }
        })
        return;
      }

      // if (data.is_end == 0) {
      //   this.countTime();   //倒计时
      // }
    });

    this.getTop();    //获取排行榜列表
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ this_page_size: 1, is_load_more: true,  });
    this.getTop(); //请求客服列表
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (this.data.is_load_more == false) {
      wx.hideNavigationBarLoading();
      return false;
    } else {
      this.setData({ this_page_size: that.data.this_page_size + 1 });
      this.getTop(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },

  /**
   * 更改获取排行榜的类型
   */
  Change: function (e) {
    var ttype = e.currentTarget.id;
    this.setData({ is_load_more: true, this_page_size: 1, c_data: [], ttype: ttype });
    this.getTop();
  },

  /**
    * 获取排行榜
    */
  getTop: function (isShowLoading) {
    var that = this;
    var this_data = that.data.c_data;
    isShowLoading = isShowLoading || false;
    var requestData = {};
    requestData.pagesize = that.data.this_page_size;
    requestData.pagenum = that.data.this_page_num;
    requestData.ttype = that.data.ttype;
    requestData.active_id = that.data.active_id;
    requestUtil.get(_DuoguanData.duoguan_host_api_url + "/index.php?s=/addon/DuoguanWenda/ApiNew/get_top.html", requestData, (data) => {
      console.log(data);
      that.setData({ my_top: data.my_top, p_num: data.p_num });
      if (data.lists == null || data.lists == '') {
        that.setData({ is_load_more: false });
      } else {
        if (data.lists.length < that.data.this_page_num) {
          that.setData({ is_load_more: false });
        }
        if (that.data.this_page_size == 1) {
          that.setData({ c_data: data.lists });
        } else {
          this_data = this_data.concat(data.lists);
          that.setData({ c_data: this_data });
        }
      }
    }, this, { completeAfter: wx.stopPullDownRefresh, isShowLoading: isShowLoading });
  },


  /**
  *带天数的倒计时
  */
  countTime: function () {
    //获取当前时间  
    var that = this;
    var date = new Date();
    var now = date.getTime();
    //设置截止时间  
    var endDate = new Date(that.data.end_time);
    var end = endDate.getTime();

    //时间差  
    var leftTime = end - now;
    //定义变量 d,h,m,s保存倒计时的时间  
    var d, h, m, s;
    if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
      m = Math.floor(leftTime / 1000 / 60 % 60);
      s = Math.floor(leftTime / 1000 % 60);
    }
    if (d == 0 && h == 0 && m == 0 && s == 0) {
      that.setData({ is_end: 1 });
      return;
    } else {
      //将倒计时赋值到时间控件中  
      that.setData({
        days: d,
        hours: h,
        minutes: m,
        seconds: s
      });
    }
    //递归每秒调用countTime方法，显示动态时间效果  
    setTimeout(that.countTime, 1000);
  },
})